package com.tongcao.cn.util;


import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * Created by Administrator on 2015/10/20 0020.
 */
public class DateUtils {
    public static final String YYMMDD = "yyyy-MM-dd";
    public static final String YYMMDD_HHmm = "yyyy-MM-dd HH:mm";
    public static final String YYMMDD_HHmmSS = "yyyy-MM-dd HH:mm:ss";
    public static final String HHmmSS = "HH:mm:ss";
    public static final String YYMMDDHHmmssSSS = "yyyyMMddHHmmssSSS";
    public static final String HHmm = "HH:mm";
    public static final String MM = "MM";
    public static final String MM_DD = "MM-dd";
    public static final String CH_MM_DD = "MM月dd日";
    public static final String CH_yyyyMMdd = "yyyy年MM月dd日";
    public static final String CH_yyyyMM = "yyyy年MM月";
    public static final String MMDD_HHMM = "MM-dd HH:mm";
    public static final String CH_yyyyMMdd_HHmmSS = "yyyy年MM月dd日 HH:mm:ss";
    public static final String CH_Md_HHmm = "M月d日 HH:mm";
    public static final String[] weekDays = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
    /**
     * yyyy-MM-dd HH:mm:ss 格式
     */
    public static final String DATE_LONG = "yyyy-MM-dd HH:mm:ss";
    /**
     * yyyy-MM-dd 格式
     */
    public static final String DATE_SHORT = "yyyy-MM-dd";
    public static final String TIME_START = "00:00:00";


    /**
     * 将字符串日期解析成日期对象
     *
     * @param date
     * @param format 取值如：DateUtil.YYMMDD
     * @return
     */
    public static Date parse(String date, String format) {
        if (StringUtils.isEmpty(date)) {
            return null;
        }
        SimpleDateFormat formater = new SimpleDateFormat(format);
        try {
            return formater.parse(date);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 获取当天结束时间 23:59:59
     *
     * @return 凌晨时间
     */
    public static Long getEndTimeOfToday() {
        return getEndTimeOfDayToDate().getTime();
    }

    public static Date getEndTimeOfDayToDate() {
        Calendar calendar = new GregorianCalendar();
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime();
    }

    /**
     * 获取当天开始时间 00:00:01
     *
     * @return
     */
    public static Long getStartTimeOfToday() {
        return getStartTimeOfTodayToDate().getTime();
    }

    public static Date getStartTimeOfTodayToDate() {
        Calendar calendar = new GregorianCalendar();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);

        return calendar.getTime();
    }

    /**
     * 将日期对象解析成字符串
     *
     * @param date
     * @param format 取值如：DateUtil.YYMMDD
     * @return
     */
    public static String format(Date date, String format) {
        SimpleDateFormat formater = new SimpleDateFormat(format);
        return formater.format(date);
    }


    public static String format(long date, String format) {
        if (date < 0) {
            return "";
        }
        if (StringUtils.isBlank(format)) {
            format = YYMMDD_HHmmSS;
        }
        SimpleDateFormat formater = new SimpleDateFormat(format);
        return formater.format(new Date(date));
    }

    public static String format(String datestr) {
        if (StringUtils.isBlank(datestr)) {
            return "";
        }
        SimpleDateFormat formater = new SimpleDateFormat(YYMMDD);
        Date date = null;
        try {
            date = formater.parse(datestr);
            return format(date, CH_yyyyMMdd);
        } catch (ParseException e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 获取当前日期，精确到天
     *
     * @return
     */
    public static Long getDayBegin() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 001);
        return cal.getTimeInMillis();
    }

    /**
     * 当前时间是否在一天的指定时间段内
     *
     * @param beginTime
     * @param endTime
     * @return
     */
    public static boolean currentTimeInTimes(long beginTime, long endTime) {
        Date date = new Date();
        int nowTime = date.getHours() * 60 + date.getMinutes();
        //产品设计不合理 导致掌柜端外卖时间可以设置跨天 20:00-04:00 兼容这种场景判断
        if (endTime < beginTime) {
            if (nowTime > beginTime || nowTime < endTime) {
                return true;
            }
        }
        if (beginTime < nowTime && endTime > nowTime) {
            return true;
        }
        return false;
    }

    public static String secondCovertDate(String second, String formater) {
        long time = Long.parseLong(second);
        Date d = new Date(time);
        SimpleDateFormat sdf = new SimpleDateFormat(formater);
        return sdf.format(d);
    }

    /**
     * 从long类型获取时间
     *
     * @param milliSecond
     * @param formater
     * @return
     */
    public static String milliSecondCovertDate(long milliSecond, String formater) {
        if (milliSecond <= 0) {
            return formatDate(null, null);
        }
        if (StringUtils.isEmpty(formater)) {
            formater = YYMMDD_HHmmSS;
        }
        return formatDate(new Date(milliSecond), formater);
    }

    /**
     * 时间格式化
     *
     * @param date
     * @param datePattern
     * @return
     */
    public static String formatDate(Date date, String datePattern) {
        if (date == null) {
            date = new Date();
        }
        if (StringUtils.isEmpty(datePattern)) {
            datePattern = YYMMDD_HHmmSS;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(datePattern);
        return sdf.format(date);
    }

    /**
     * 返回当前时间的指定格式
     *
     * @param datePattern
     * @return
     */
    public static String formatDate(String datePattern) {
        return formatDate(null, datePattern);
    }


    public static String getWeek(String second) {
        long time = Long.parseLong(second);
        Date date = new Date(time);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0) {
            w = 0;
        }
        return weekDays[w];
    }

    /**
     * 获取下一天的零点时间
     *
     * @return 2017-09-10 00：00：00
     */
    public static String getNextDayZeroTime() {
        return getNextDay() + " " + TIME_START;
    }

    /**
     * 获取今天的下一天
     *
     * @return 2017-09-10
     */
    public static String getNextDay() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, 1);
        String yesterday = new SimpleDateFormat(DATE_SHORT).format(cal.getTime());
        return yesterday;
    }

    /**
     * 获取下一天零点到系统当前时间的时间差值，单位毫秒
     *
     * @return
     */
    public static long getCurrentTimeToNextDayZeroTimeMills() {
        Date nextDayZeroTime = strToDate(getNextDayZeroTime(), DATE_LONG);

        return nextDayZeroTime.getTime() - System.currentTimeMillis();
    }

    public static Date strToDate(String str, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            return sdf.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}
